﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using ServiceWeb.WebAdmin;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class DeviceModelController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        // GET: Mager_Department
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " "; // and m_attribute<>4 
            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and m_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));

            }
            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            if (!string.IsNullOrEmpty(httpRequest.status))
            {
                //设置状态
                sqlwhere = sqlwhere + " and m_status=@status";
                sugarParameters.Add(new SugarParameter("status", httpRequest.status));
            }


            string sql = "select  * from iot_device_model inner join iot_device_model_type on m_type_guid=t_guid where 1= 1  " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "m_id desc", ref totalRecord);


            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码
            return View(httpRequest);
        }
        public IActionResult edit(HttpReqModel httpReqModel)
        {
            if (httpReqModel.order == "upd")
            {
                ViewBag.m_guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.m_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }

        public IActionResult variable(HttpReqModel httpReqModel)
        {
            ViewBag.mv_model_guid = httpReqModel.modelguid.ToString();
            if (httpReqModel.order == "upd")
            {
                ViewBag.mv_guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.mv_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }



        public IActionResult command(HttpReqModel httpReqModel)
        {
            ViewBag.dc_model_guid = httpReqModel.modelguid.ToString();
            if (httpReqModel.order == "upd")
            {
                ViewBag.dc_guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.dc_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }
        public RespResultData saveAll(IFormCollection FC, HttpReqModel httpRequest, iot_device_model req, HttpReqModel httpReqModel)
        {

            new DBIotDeviceModel().saveAll(FC, httpRequest, LoginUserInfo, req, httpReqModel);
            return RespResult.Sucess();
        }

        public RespResultData getModel(HttpReqModel httpReqModel)
        {

            return RespResult.Sucess(new DBIotDeviceModel().getModel(httpReqModel.guid));
        }
        public RespResultData getModelFile(HttpReqModel httpReqModel)
        {
            return RespResult.Sucess(new DBIotDeviceModelFile().getModelFiles(httpReqModel.guid, httpReqModel.type));
        }

        public RespResultData getModelTypeList()
        {
            return RespResult.Sucess(new DBIotDeviceModelType().getModelTypeList());
        }
        public RespResultData getAgreementList()
        {
            return RespResult.Sucess(new DBIotDeviceAgreement().getAgreementList());
        }
        public RespResultData getVariableRelation(HttpReqModel httpReqModel)
        {
            return RespResult.Sucess(new DBIotDeviceModelVariableRelation().getVariableRelation(httpReqModel.guid));
        }
        public RespResultData getVariableList()
        {
            return RespResult.Sucess(new DBIotDeviceModelVariable().getVariableList());
        }
        public RespResultData saveAllVariable(IFormCollection FC, HttpReqModel httpRequest, iot_device_model_variable_relation req)
        {
            new DBIotDeviceModelVariableRelation().saveAll(FC, httpRequest, LoginUserInfo, req);
            return RespResult.Sucess();
        }

        public RespResultData getEditRelationList(HttpReqModel httpReqModel)
        {

            List<Dictionary<string, string>> list = new DBIotDeviceModelVariableRelation().getEditRelationList(LoginUserInfo.userCompanyGuid, httpReqModel, httpReqModel.type);
            return RespResult.Sucess(list);


        }

        public RespResultData delRelation(IFormCollection FC, HttpReqModel httpReqModel)
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            iot_device_model_variable_relation item = ef.iot_device_model_variable_relation.Where(c => c.mv_guid == httpReqModel.guid).FirstOrDefault();
            if (item != null)
            {
                ef.iot_device_model_variable_relation.Remove(item);
                ef.SaveChanges();
            }

            return RespResult.Sucess();
        }
        public RespResultData getCommandByModel(HttpReqModel httpReqModel)
        {
            List<iot_device_command> list = new DBIotDeviceCommand().getCommandByModel(httpReqModel.modelguid);
            return RespResult.Sucess(list);
        }
        public RespResultData saveAllCommand(IFormCollection FC, HttpReqModel httpRequest, iot_device_command req)
        {
            new DBIotDeviceCommand().saveAll(FC, httpRequest, LoginUserInfo, req);
            return RespResult.Sucess();
        }
        public RespResultData delcommand(IFormCollection FC, HttpReqModel httpReqModel)
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            iot_device_command item = ef.iot_device_command.Where(c => c.dc_guid == httpReqModel.guid).FirstOrDefault();
            if (item != null)
            {
                ef.iot_device_command.Remove(item);
                ef.SaveChanges();
            }

            return RespResult.Sucess();
        }
        public RespResultData loadInfoFileTemp(HttpReqModel httpReqModel)
        {
            new ServiceIotDeviceModelFile().loadInfoFileTemp(httpReqModel.guid);
            return RespResult.Sucess();
        }

        public RespResultData saveInfoFileTemp(HttpReqModel httpReqModel)
        {
            new ServiceIotDeviceModelFile().saveInfoFileTemp(httpReqModel.guid, httpReqModel.name, httpReqModel.fileurl);
            return RespResult.Sucess();
        }
        public RespResultData getInfoFileTemp(HttpReqModel httpReqModel)
        {
            return RespResult.Sucess(new ServiceIotDeviceModelFile().getInfoFileTemp(httpReqModel.guid));
        }
        public RespResultData delInfoFileTemp(HttpReqModel httpReqModel)
        {
            return RespResult.Sucess(new ServiceIotDeviceModelFile().delInfoFileTemp(httpReqModel.guid, httpReqModel.fileurl));
        }

    }
}